import { Component, _decorator } from "cc";
import { getOrientation,DeviceOrientation } from "../../Const";
const {ccclass,property}=_decorator
@ccclass
export default class Orientation extends Component{

    protected onLoad(): void {
        //view.setResizeCallback(function() {})
        //view.setOrientation
    }
    /*protected onEnable(): void {
        //判断手机横竖屏状态：
        //window.addEventListener("onorientationchange" in window ? "orientationchange" : "resize",this.onOrientation,false)
        // 监听设备方向变化事件  
        //systemEvent.setDeviceOrientation(this.handleDeviceOrientation);
        //window.addEventListener("resize",this.onOrientation,false)
        //console.log('添加deviceorientation监听')
        //window.addEventListener("deviceorientation", this.onOrientation.bind(this), true)
    }
    protected onDisable(): void {
        //window.removeEventListener("onorientationchange" in window ? "orientationchange" : "resize",this.onOrientation,false)
        //window.removeEventListener("resize",this.onOrientation,false)
        //console.log('移除deviceorientation监听,'+this.node.name)
        //window.removeEventListener("deviceorientation", this.onOrientation.bind(this), true)
    }*/
    get orientation():DeviceOrientation{
        const angle= getOrientation()
        if (angle == 180 || angle== 0) {
            return DeviceOrientation.Portrait
        }
        if (angle== 90 || angle== -90) {
            return DeviceOrientation.Landscape
        }
    }
    protected onPortraitPrimary(){
        //console.log('竖屏')
    }
    protected onLandscapePrimary(){
        //console.log('横屏')
    }
    protected onOrientation(e?:DeviceOrientationEvent){
        console.log('Orientation.onOrientation')
        //console.log(e)
        if(this.orientation==DeviceOrientation.Portrait){
            this.onPortraitPrimary()
        }else{
            this.onLandscapePrimary()
        }
        /*if(this.node&&this.node.active){
            const angle = getOrientation()
            if (angle == 180 || angle== 0) {
                this.onPortraitPrimary()
            }
            if (angle== 90 || angle== -90) {
                this.onLandscapePrimary()
            }
        }*/
    }
}